package Easy;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class Employee {
    public int id;
    public int importance;
    public List<Integer> subordinates;
};

public class Solution690 {
    public int getImportance(List<Employee> employees, int id) {
        // 先找到员工id
        Employee start = findUseId(employees, id);
        if (start == null) return 0;
        int sum = 0;
        if (start.subordinates == null) return start.importance;
        Queue queue = new LinkedList();
        sum += start.importance;
        for (int i : start.subordinates) {
            queue.offer(i);
        }
        while (!queue.isEmpty()) {
            int poll = (int) queue.poll();
            Employee e = findUseId(employees, poll);
            sum += e.importance;
            for (int i : e.subordinates) {
                queue.offer(i);
            }
        }
        return sum;
    }

    public static Employee findUseId(List<Employee> employees, int id) {
        for (Employee e : employees) {
            if (e.id == id) {
                return e;
            }
        }
        return null;
    }
}
